<?php
/**
 * Date: 10/22/12
 * Time: 10:12 PM
 * User: Amitav Roy
 */
class Transaction_m extends CI_Model{
	// Name of the transaction table
	protected $expense_table = 'expense';

	// User id stored from session.
	protected $user_id;

	// Entity type
	protected $entity_type;
  
  // Per page count
  protected $per_page = 20;

	function __construct() {
		parent::__construct();

		// Getting the user id.
		$this->user_id = $this->session->userdata('uid');

		// Define entity type
		$this->entity_type = 'expense';
	}

	/**
	 * This function is to insert a transaction to the trans table.
	 * The description is added elsewere.
	 */
	function add_transaction() {
		// Assigning the post variables.
		$data['uid'] = $this->user_id;
		$data['amount'] = $this->input->post('amount');
		$data['date'] = $this->input->post('date');

		$this->db->insert($this->expense_table, $data);

		// Need to insert the description
		$expense_id = $this->db->insert_id();
		$description = $this->input->post('particulars');
		$this->global_model->global_add_description($description, $expense_id, $this->entity_type);

		// Inserting the term vs entity
		$term = $this->input->post('terms');
		$this->global_model->global_add_term($term, $expense_id);
	}

	/**
	 * This is the base query which will be used everywhere,
	 * where I need to display the expense transactions.
	 *
	 * This function should not be called directly.
	 * It should pass through base_view_expense_getter.
	 */
	protected function base_view_expense() {
    // List of fields required.
		$fields = array(
			'e.date',
      'concat("Rs ", e.amount) as amount',
			'u.display_name',
			'd.description',
			't.name',
			'e.eid'
		);

		$this->db->select(implode(",", $fields),FALSE);
		$this->db->from($this->expense_table . ' e');

    // Joining all the required tables.
		$this->db->join('users u','u.uid = e.uid');
		$this->db->join('description d', 'e.eid=d.eid');
		$this->db->join('term_entity te','te.eid=e.eid');
		$this->db->join('term t','t.tid=te.tid');
	}

	/**
	 * Get function for the protected base_view_expense
	 * base query. This is used every where including this class.
	 */
	function base_view_expense_getter() {
		return $this->base_view_expense();
	}

  /**
   * This is the main query which extends the base query and 
   * is used in the view page to get the expense list from the DB
   * and return the result.
   */
	function view_expenses() {
		// Calling the base query.
		$this->base_view_expense_getter();

		// Pagination offset
		$offset = $this->uri->segment(3, 0);
		$this->db->limit($this->per_page, $offset);

		// Ordering the result
		$this->db->order_by('e.date','DESC');
		$this->db->order_by('e.eid','DESC');

		// Firing the query
		$query = $this->db->get();
    
    // dsm($this->db->last_query());

		return $query;
	}

  /**
   * This function is required to get the conunt of expense in the DB.
   * This is very important to get the pager count correct on the listing page.
   */
	function expense_count() {
		// Calling the base query.
		$this->base_view_expense();

		// Firing the query
		$query = $this->db->get();

		return $query->num_rows();
	}

	/**
	 * This is the function to get the details of the
	 * individual expense entry.
	 */
	function expense_entry_details($entry_id) {
		// Getting the base query
		$this->base_view_expense_getter();

		//Joining the category table
		$this->db->join('category c','c.cid = t.cid');

		// Adding the category name
		$this->db->select('c.name as cat, c.cid as cid, te.tid as termid, e.amount as famount');

		// Adding the clause
		$this->db->where('e.eid',$entry_id);

		$query = $this->db->get();

		return $query->result();
	}

	function update_expense($data) {
		// Initialising the arrays
		$expense_array = array();
		$description_array = array();
		$term_entity_array = array();

		// Building the data for expense row.
		$expense_array['eid'] = $data['expense_id'];
		$expense_array['date'] = $data['date'];
		$expense_array['amount'] = $data['amount'];

		// Updating the row.
		$this->db->where('eid',$expense_array['eid']);
		$this->db->update($this->expense_table,$expense_array);

		// Building the data for description row.
		$description_array['eid'] = $data['expense_id'];
		$description_array['description'] = $data['particulars'];

		// Updating the row.
		$this->db->where('eid',$description_array['eid']);
		$this->db->update('description',$description_array);

		// Getting the term based on the category.
		if (isset($data['terms'])) {
			// If user change the term and category
			$term_entity['tid'] = $data['terms'];
		}
		else {
			// Else hidden field
			$term_entity['tid'] = $data['termid'];
		}

		$term_entity['eid'] = $data['expense_id'];

		// Updating the row.
		$this->db->where('eid',$term_entity['eid']);
		$this->db->update('term_entity',$term_entity);
	}
}

/* End of file transaction_m.php */
/* Location: ./application/controllers/transaction_m.php */